MYOWD=/data/data/com.termux/files/home/gitee/emp
[[ -z $MYOWD ]] && MYOWD='.'
Base163Dir=$MYOWD
Baseurl='http://47.99.157.36:50300'
mkdir -p "$Base163Dir/.163"
input_error(){
	echo '参数 输错 啦。🍎'
	exit
}
api(){
	case $1 in
		recommend)
			case $2 in
				song)arg="recommend/songs" ;;
				mv)arg="video/timeline/recommend" ;;
			esac ;;
		record)arg="user/record?uid=$2" ;;
		search)
			case $2 in
				list)Type=1000;;
				song)Type=1;;
				mv)Type=1004;;
				all)Type=1018;;
			esac
			cmd="-G $Baseurl/cloudsearch?type=$Type --data-urlencode ";;
		list)
			case $2 in
				lists)
					arg="user/playlist?uid=$3" ;;
				songs)
					arg="playlist/track/all?id=$3" ;;
			esac ;;
		geturl)
			case $2 in
				song)
					arg="song/url/vl?id=$3&level=lossless" ;;
				mv)
					arg="mv/url?id=$3" ;;
			esac ;;
		login)
			case $2 in
				phone)
					arg="login/cellphone?phone=$3&password=$4" ;;
				send)
					arg="captcha/sent?phone=$3" ;;
				captcha)
					arg="login/cellphone?phone=$3&captcha=$4" ;;
				email)
					arg="login?email=$3&password=$4" ;;
				status)
					arg="login/status" ;;
				refresh)
					arg="login/refresh" ;;
				logout)
					arg="logout" ;;
			esac ;;
		get)
			arg="$4" ;;
		refresh)
			case $# in
				1)f="*" ;;
				2)f="$2.*" ;;
				3)f="$2.$3.*" ;;
				4|5)f="$2.$3.$4" ;;
			esac
			rm "$Base163Dir/.163/$f" ;;
		*)
			input_error ;;
	esac
	if [[ $1 != refresh ]];then
		case "$#" in
			0)input_error ;;
			1)f="$1" ;;
			2)f="${1}.$2" ;;
			3|4|5)f="${1}.${2}.${3}" ;;
		esac
		file="$Base163Dir/.163/$f"
		if [[ ! -f "$file" || $1 = 'geturl' ]];then
			if [[ $1 == 'geturl' ]];then 
				curl -s "$Baseurl/$arg"
				exit
			fi

			if [[ $1 = 'search' ]];then
				curl $cmd "keywords=$3" > "$file"
			else
				curl -s "$Baseurl/$arg" > "$file"
				echo $cmd
			fi
		fi
		cat "$file"
	fi
}
geturl(){
	case $1 in
		mv)api="mv $2"; jq=".data.url" ;;
		song)api="song $2"; jq=".data|.[].url" ;;
		size)api="song $2"; jq='.data|.[].size' ;;
		mvsize)api="mv $2"; jq=".data.size" ;;
		*)input_error ;;
	esac
	url=$(api geturl $api|jq "$jq" -r)
	if [[ $url == null && $1 == song && $3 != 0 ]];then
		api refresh geturl $api
		url=$(api geturl 'mv' $3|jq ".data.url" -r)
	fi
	echo $url
}
case $1 in
	api|geturl)$1 $2 $3 $4 $5 ;;
esac
